/// @tags: CRT
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>

namespace BlueQuantum {

typedef long long ll;
typedef std::pair<int, int> pii;

const int U = 1e9;
const int MG = 10000005, MS = 3175;
const int N = 1e3 + 5;

template <typename T>
inline T &read(T &x) {
  x = 0;
  bool f = false;
  short ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = true;
    ch = getchar();
  }
  while (isdigit(ch)) x = x * 10 + (ch ^ '0'), ch = getchar();
  if (f) x = -x;
  return x;
}

#define Fail return puts("Impossible"), 0
#define mp std::make_pair

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

template <typename T>
T exgcd(T a, T b, T &x, T &y) {
  if (!b) return x = 1, y = 0, a;
  int d = exgcd(b, a % b, y, x);
  return y -= a / b * x, d;
}

int buk[MG], stk[MS];

inline int BSGS(int a, int b, int m) {
  int S = sqrt(m - 1) + 1;
  int A = 1, f = -1, t = 0;
  for (int i = 0; i < S; ++i) {
    buk[stk[++t] = (ll)b * A % m] = i;
    A = (ll)A * a % m;
  }
  int C = 1;
  for (int i = 1; !~f && i <= S; ++i)
    if (~buk[C = (ll)C * A % m]) f = i * S - buk[C];
  while (t) buk[stk[t--]] = -1;
  return f;
}

inline pii ExBSGS(int a, int b, int m) {
  int o = 0, A = 1 % m, d = 1, nd, x, y;
  while (1) {
    if (d == (nd = gcd((ll)A * a % m, m))) break;
    if (A == b) return mp(o, -1);
    ++o, A = (ll)A * a % m, d = nd;
  }
  if (b % d) return mp(-1, -1);
  m /= d, b /= d, A /= d;
  exgcd(A, m, x, y);
  b = 1ll * b * (x + m) % m;
  x = BSGS(a, b, m);
  if (!~x) return mp(-1, -1);
  y = BSGS(a, 1 % m, m);
  return mp(x % y + o, y);
}

inline bool Combine(ll &a1, ll &m1, ll a2, ll m2) {
  ll k1, k2, g = exgcd(m1, m2, k1, k2);
  if ((a2 - a1) % g) return 0;
  a1 += (k1 * ((a2 - a1) / g) % m2 + m2) * m1 % (m1 / g * m2);
  return a1 %= m1 *= m2 / g, 1;
}

int n;
int k[N], r[N], g[N];
int x[N], q[N], X, MaxX;

inline int main() {
  memset(buk, -1, sizeof(buk)), X = -1;
  read(n);
  for (int i = 1; i <= N; ++i) {
    read(k[i]), read(g[i]), read(r[i]);
    pii ans = ExBSGS(k[i] % g[i], r[i] % g[i], g[i]);
    x[i] = ans.first, q[i] = ans.second;
    if (!~x[i]) Fail;
    if (!~q[i]) X = x[i];
    MaxX = std::max(MaxX, x[i]);
  }
  if (~X) {
    for (int i = 1; i <= N; ++i) {
      if (~q[i] && (X < x[i] || (X - x[i]) % q[i])) Fail;
      if (!~q[i] && X != x[i]) Fail;
    }
    return printf("%d\n", X), 0;
  }
  ll P = 0, Q = 1;
  for (int i = 1; i <= N; ++i) {
    if (Q > U && (P - x[i]) % q[i]) Fail;
    if (Q <= U && !Combine(P, Q, x[i] % q[i], q[i])) Fail;
  }
  if (P < MaxX) P += ((MaxX - P - 1) / Q + 1) * Q;
  if (P > U) Fail;
  printf("%lld\n", P);
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("P5345 【XR-1】快乐肥宅.in", "r", stdin);
  freopen("P5345 【XR-1】快乐肥宅.out", "w", stdout);
#endif
#endif

  return BlueQuantum::main();
}